﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Diagnostics.CodeAnalysis
Imports System.Drawing
Imports System.Globalization
Imports System.Reflection
Imports System.Runtime.CompilerServices
Imports System.Text
Imports System.Windows.Forms
Imports System.Workflow.Activities.Rules

Namespace System.Workflow.Activities.Rules.Design
    Friend Class IntellisenseTextBox
        Inherits TextBox
        ' Events
        Public Custom Event PopulateAutoCompleteList As EventHandler([Of] AutoCompletionEventArgs)
        Public Custom Event PopulateToolTipList As EventHandler([Of] AutoCompletionEventArgs)

        ' Methods
        Public Sub New()
            Me.InitializeComponent
            MyBase.AcceptsReturn = True
            Me.listBoxAutoComplete.FullRowSelect = True
            Me.listBoxAutoComplete.MultiSelect = False
            Me.listBoxAutoComplete.SmallImageList = Me.autoCompletionImageList
            Me.listBoxAutoComplete.LargeImageList = Me.autoCompletionImageList
            Me.listBoxAutoComplete.View = View.Details
            Me.listBoxAutoComplete.HeaderStyle = ColumnHeaderStyle.None
            Me.listBoxAutoComplete.Columns.Add(Messages.No, Me.listBoxAutoComplete.Size.Width)
            Me.listBoxAutoComplete.CausesValidation = False
            Me.listBoxAutoComplete.Sorting = SortOrder.Ascending
            Me.listBoxAutoComplete.Visible = False
            AddHandler MyBase.KeyPress, New KeyPressEventHandler(AddressOf Me.IntellisenseTextBox_KeyPress)
            AddHandler MyBase.HandleCreated, New EventHandler(AddressOf Me.IntellisenseTextBox_HandleCreated)
        End Sub

        Private Shared Sub AppendParameterInfo(ByVal toolTipText As StringBuilder, ByVal parameterInfo As ParameterInfo, ByVal isLastParameter As Boolean)
            Dim parameterType As Type = parameterInfo.ParameterType
            If (Not parameterType Is Nothing) Then
                If parameterType.IsByRef Then
                    If parameterInfo.IsOut Then
                        toolTipText.Append("out ")
                    Else
                        toolTipText.Append("ref ")
                    End If
                    parameterType = parameterType.GetElementType
                ElseIf (isLastParameter AndAlso parameterType.IsArray) Then
                    Dim customAttributes As Object() = parameterInfo.GetCustomAttributes(GetType(ParamArrayAttribute), False)
                    If ((Not customAttributes Is Nothing) AndAlso (customAttributes.Length > 0)) Then
                        toolTipText.Append("params ")
                    End If
                End If
                toolTipText.Append(RuleDecompiler.DecompileType(parameterType))
                toolTipText.Append(" ")
            End If
            toolTipText.Append(parameterInfo.Name)
        End Sub

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If (disposing AndAlso (Not Me.components Is Nothing)) Then
                Me.components.Dispose
            End If
            MyBase.Dispose(disposing)
        End Sub

        Friend Sub HideIntellisenceDropDown()
            Me.listBoxAutoComplete.Hide
            Me.toolTip.Hide(Me)
        End Sub

        Private Sub InitializeComponent()
            Me.components = New Container
            Dim manager As New ComponentResourceManager(GetType(IntellisenseTextBox))
            Me.autoCompletionImageList = New ImageList(Me.components)
            Me.toolTip = New ToolTip(Me.components)
            MyBase.SuspendLayout
            Me.autoCompletionImageList.ImageStream = DirectCast(manager.GetObject("autoCompletionImageList.ImageStream"), ImageListStreamer)
            Me.autoCompletionImageList.TransparentColor = Color.Magenta
            Me.autoCompletionImageList.Images.SetKeyName(0, "")
            Me.autoCompletionImageList.Images.SetKeyName(1, "")
            Me.autoCompletionImageList.Images.SetKeyName(2, "")
            Me.autoCompletionImageList.Images.SetKeyName(3, "")
            Me.autoCompletionImageList.Images.SetKeyName(4, "")
            Me.autoCompletionImageList.Images.SetKeyName(5, "")
            Me.autoCompletionImageList.Images.SetKeyName(6, "")
            Me.autoCompletionImageList.Images.SetKeyName(7, "")
            Me.autoCompletionImageList.Images.SetKeyName(8, "")
            Me.autoCompletionImageList.Images.SetKeyName(9, "")
            Me.autoCompletionImageList.Images.SetKeyName(10, "")
            Me.autoCompletionImageList.Images.SetKeyName(11, "")
            Me.autoCompletionImageList.Images.SetKeyName(12, "")
            Me.autoCompletionImageList.Images.SetKeyName(13, "")
            Me.autoCompletionImageList.Images.SetKeyName(14, "Keyword.bmp")
            Me.autoCompletionImageList.Images.SetKeyName(15, "MethodExtension.bmp")
            Me.toolTip.AutomaticDelay = 0
            Me.toolTip.UseAnimation = False
            AddHandler MyBase.Enter, New EventHandler(AddressOf Me.IntellisenseTextBox_Enter)
            AddHandler MyBase.MouseClick, New MouseEventHandler(AddressOf Me.IntellisenseTextBox_MouseClick)
            AddHandler MyBase.Leave, New EventHandler(AddressOf Me.IntellisenseTextBox_Leave)
            AddHandler MyBase.KeyDown, New KeyEventHandler(AddressOf Me.IntellisenseTextBox_KeyDown)
            MyBase.ResumeLayout(False)
        End Sub

        Private Sub IntellisenseTextBox_Enter(ByVal sender As Object, ByVal e As EventArgs)
            If (Me.oldSelectionStart >= 0) Then
                MyBase.SelectionStart = Me.oldSelectionStart
            End If
        End Sub

        Private Sub IntellisenseTextBox_HandleCreated(ByVal sender As Object, ByVal e As EventArgs)
            If (Not MyBase.TopLevelControl Is Nothing) Then
                MyBase.TopLevelControl.Controls.Add(Me.listBoxAutoComplete)
                AddHandler Me.listBoxAutoComplete.DoubleClick, New EventHandler(AddressOf Me.listBoxAutoComplete_DoubleClick)
                AddHandler Me.listBoxAutoComplete.SelectedIndexChanged, New EventHandler(AddressOf Me.listBoxAutoComplete_SelectedIndexChanged)
                AddHandler Me.listBoxAutoComplete.Enter, New EventHandler(AddressOf Me.listBoxAutoComplete_Enter)
            End If
        End Sub

        <SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity")> _
        Private Sub IntellisenseTextBox_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
            Dim text As String = Me.Text
            Dim selectionStart As Integer = MyBase.SelectionStart
            Dim selectionLength As Integer = Me.SelectionLength
            Dim builder As New StringBuilder([text].Substring(selectionStart, selectionLength))
            Dim builder2 As New StringBuilder([text].Substring(0, selectionStart))
            builder2.Append([text].Substring((selectionStart + selectionLength)))
            Trace.WriteLine(String.Format(CultureInfo.CurrentCulture, "KeyCode:{0}, KeyData:{1}, KeyValue:{2}", New Object() { e.KeyCode, e.KeyData, e.KeyValue }))
            Me.toolTip.Hide(Me)
            If (e.KeyData = (Keys.Control Or Keys.Space)) Then
                If Not Me.listBoxAutoComplete.Visible Then
                    Me.UpdateIntellisenceDropDown(Me.Text.Substring(0, (selectionStart - Me.CurrentPrefix.Length)))
                    Me.ShowIntellisenceDropDown(selectionStart)
                    Me.UpdateAutoCompleteSelection(Me.CurrentPrefix)
                    e.SuppressKeyPress = True
                    e.Handled = True
                End If
            ElseIf (e.KeyCode = Keys.Back) Then
                If (Me.Text.Length > 0) Then
                    If ((builder.Length = 0) AndAlso (selectionStart > 0)) Then
                        builder.Append(builder2.Chars((selectionStart - 1)))
                        builder2.Length -= 1
                    End If
                    If (Me.CurrentPrefix.Length <= 1) Then
                        Me.HideIntellisenceDropDown
                    End If
                    If (builder.ToString.IndexOfAny(". ()[]" & ChrW(9) & ChrW(10).ToCharArray) >= 0) Then
                        Me.HideIntellisenceDropDown
                    ElseIf Me.listBoxAutoComplete.Visible Then
                        Me.UpdateAutoCompleteSelection(Me.CurrentPrefix.Substring(0, (Me.CurrentPrefix.Length - 1)))
                    End If
                End If
            ElseIf (e.KeyCode = Keys.Up) Then
                If Me.listBoxAutoComplete.Visible Then
                    If ((Me.listBoxAutoComplete.SelectedIndices.Count > 0) AndAlso (Me.listBoxAutoComplete.SelectedIndices.Item(0) > 0)) Then
                        Me.listBoxAutoComplete.Items.Item((Me.listBoxAutoComplete.SelectedIndices.Item(0) - 1)).Selected = True
                        Me.listBoxAutoComplete.Items.Item(Me.listBoxAutoComplete.SelectedIndices.Item(0)).Focused = True
                    End If
                    e.Handled = True
                End If
            ElseIf (e.KeyCode = Keys.Down) Then
                If Me.listBoxAutoComplete.Visible Then
                    If (Me.listBoxAutoComplete.SelectedIndices.Count = 0) Then
                        If (Me.listBoxAutoComplete.Items.Count > 0) Then
                            Me.listBoxAutoComplete.Items.Item(0).Selected = True
                            Me.listBoxAutoComplete.Items.Item(0).Focused = True
                        End If
                    ElseIf (Me.listBoxAutoComplete.SelectedIndices.Item(0) < (Me.listBoxAutoComplete.Items.Count - 1)) Then
                        Me.listBoxAutoComplete.Items.Item((Me.listBoxAutoComplete.SelectedIndices.Item(0) + 1)).Selected = True
                        Me.listBoxAutoComplete.Items.Item(Me.listBoxAutoComplete.SelectedIndices.Item(0)).Focused = True
                    End If
                    e.Handled = True
                End If
            ElseIf ((((e.KeyCode <> Keys.ShiftKey) AndAlso (e.KeyCode <> Keys.ControlKey)) AndAlso (e.KeyCode <> Keys.OemPeriod)) AndAlso (((((e.KeyValue < &H30) OrElse ((e.KeyValue >= &H3A) AndAlso (e.KeyValue <= &H40))) OrElse ((e.KeyValue >= &H5B) AndAlso (e.KeyValue <= &H60))) OrElse (e.KeyValue > &H7A)) AndAlso ((e.KeyData <> (Keys.Shift Or Keys.OemMinus)) AndAlso Me.listBoxAutoComplete.Visible))) Then
                If ((e.KeyCode = Keys.Return) OrElse (e.KeyCode = Keys.Space)) Then
                    Me.SelectItem
                    e.Handled = True
                End If
                Me.HideIntellisenceDropDown
            End If
        End Sub

        Private Sub IntellisenseTextBox_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
            Dim text As String = Me.Text
            Dim selectionStart As Integer = MyBase.SelectionStart
            Dim selectionLength As Integer = Me.SelectionLength
            Dim builder As New StringBuilder([text].Substring(0, selectionStart))
            builder.Append([text].Substring((selectionStart + selectionLength)))
            Dim keyChar As Char = e.KeyChar
            Select Case keyChar
                Case "."c
                    If Me.listBoxAutoComplete.Visible Then
                        Me.SelectItem
                        Me.HideIntellisenceDropDown
                        Me.IntellisenseTextBox_KeyPress(sender, e)
                        Return
                    End If
                    builder.Insert(selectionStart, "."c)
                    Me.UpdateIntellisenceDropDown(builder.ToString.Substring(0, (selectionStart + 1)))
                    Me.ShowIntellisenceDropDown(selectionStart)
                    Me.IntellisenseTextBox_KeyDown(sender, New KeyEventArgs(Keys.Down))
                    Return
                Case "("c
                    If Me.listBoxAutoComplete.Visible Then
                        Me.SelectItem
                        Me.HideIntellisenceDropDown
                        Me.IntellisenseTextBox_KeyPress(sender, e)
                        Return
                    End If
                    builder.Insert(selectionStart, "("c)
                    Me.ShowToolTip(selectionStart, builder.ToString.Substring(0, (selectionStart + 1)))
                    Return
            End Select
            If ((Not Me.listBoxAutoComplete.Visible AndAlso (Me.CurrentPrefix.Length = 0)) AndAlso (((keyChar = "_"c) OrElse Char.IsLetter(keyChar)) OrElse (Char.GetUnicodeCategory(keyChar) = UnicodeCategory.LetterNumber))) Then
                builder.Insert(selectionStart, keyChar)
                Me.UpdateIntellisenceDropDown(builder.ToString.Substring(0, (selectionStart + 1)))
                Me.ShowIntellisenceDropDown(selectionStart)
                If Me.listBoxAutoComplete.Visible Then
                    Me.IntellisenseTextBox_KeyDown(sender, New KeyEventArgs(Keys.Down))
                End If
            ElseIf Me.listBoxAutoComplete.Visible Then
                builder.Insert(selectionStart, keyChar)
                Me.UpdateAutoCompleteSelection((Me.CurrentPrefix & keyChar))
            End If
        End Sub

        Private Sub IntellisenseTextBox_Leave(ByVal sender As Object, ByVal e As EventArgs)
            Me.oldSelectionStart = MyBase.SelectionStart
            Me.toolTip.Hide(Me)
            If (Not Me.listBoxAutoComplete.Focused AndAlso Not Me.Focused) Then
                Me.listBoxAutoComplete.Visible = False
            End If
        End Sub

        Private Sub IntellisenseTextBox_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
            Me.HideIntellisenceDropDown
        End Sub

        Private Sub listBoxAutoComplete_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
            If (Me.listBoxAutoComplete.SelectedItems.Count = 1) Then
                Me.SelectItem
                Me.HideIntellisenceDropDown
            End If
        End Sub

        Private Sub listBoxAutoComplete_Enter(ByVal sender As Object, ByVal e As EventArgs)
            MyBase.CausesValidation = False
            MyBase.Focus
            MyBase.CausesValidation = True
        End Sub

        Private Sub listBoxAutoComplete_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            Dim item As ListViewItem
            For Each item In Me.listBoxAutoComplete.Items
                If item.Selected Then
                    item.ForeColor = SystemColors.HighlightText
                    item.BackColor = SystemColors.Highlight
                    item.EnsureVisible
                Else
                    item.ForeColor = SystemColors.ControlText
                    item.BackColor = SystemColors.Window
                End If
            Next
        End Sub

        <SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity"), SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")> _
        Private Sub PopulateListBox(ByVal list As ICollection)
            Me.listBoxAutoComplete.Items.Clear
            If ((Not list Is Nothing) AndAlso (list.Count > 0)) Then
                Dim obj2 As Object
                For Each obj2 In list
                    Dim item As ListViewItem = Nothing
                    If TypeOf obj2 Is String Then
                        item = New ListViewItem([TryCast](obj2,String)) { _
                            .ImageIndex = 0 _
                        }
                    ElseIf TypeOf obj2 Is IntellisenseKeyword Then
                        item = New ListViewItem(DirectCast(obj2, IntellisenseKeyword).Name) { _
                            .ImageIndex = 14 _
                        }
                    ElseIf TypeOf obj2 Is MemberInfo Then
                        item = New ListViewItem(DirectCast(obj2, MemberInfo).Name)
                        If TypeOf obj2 Is PropertyInfo Then
                            Dim getMethod As MethodInfo = DirectCast(obj2, PropertyInfo).GetGetMethod(True)
                            If (getMethod Is Nothing) Then
                                getMethod = DirectCast(obj2, PropertyInfo).GetSetMethod(True)
                            End If
                            If getMethod.IsPublic Then
                                item.ImageIndex = 6
                            ElseIf getMethod.IsPrivate Then
                                item.ImageIndex = 7
                            ElseIf ((getMethod.IsFamily OrElse getMethod.IsFamilyAndAssembly) OrElse getMethod.IsFamilyOrAssembly) Then
                                item.ImageIndex = 9
                            Else
                                item.ImageIndex = 8
                            End If
                        ElseIf TypeOf obj2 Is FieldInfo Then
                            Dim info2 As FieldInfo = DirectCast(obj2, FieldInfo)
                            If info2.IsPublic Then
                                item.ImageIndex = 10
                            ElseIf info2.IsPrivate Then
                                item.ImageIndex = 11
                            ElseIf ((info2.IsFamily OrElse info2.IsFamilyAndAssembly) OrElse info2.IsFamilyOrAssembly) Then
                                item.ImageIndex = 13
                            Else
                                item.ImageIndex = 12
                            End If
                        ElseIf TypeOf obj2 Is ExtensionMethodInfo Then
                            item.ImageIndex = 15
                        ElseIf TypeOf obj2 Is MethodInfo Then
                            Dim info3 As MethodInfo = DirectCast(obj2, MethodInfo)
                            If info3.IsPublic Then
                                item.ImageIndex = 2
                            ElseIf info3.IsPrivate Then
                                item.ImageIndex = 3
                            ElseIf ((info3.IsFamily OrElse info3.IsFamilyAndAssembly) OrElse info3.IsFamilyOrAssembly) Then
                                item.ImageIndex = 5
                            Else
                                item.ImageIndex = 4
                            End If
                        ElseIf TypeOf obj2 Is Type Then
                            item.ImageIndex = 1
                        End If
                    End If
                    Me.listBoxAutoComplete.Items.Add(item)
                Next
            End If
            Me.listBoxAutoComplete.Sort
            If (Me.listBoxAutoComplete.Items.Count > 0) Then
                Me.listBoxAutoComplete.Columns.Item(0).Width = -2
                Me.listBoxAutoComplete.Size = New Size((Me.listBoxAutoComplete.Items.Item(0).Bounds.Width + 30), &H48)
            End If
        End Sub

        Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
            If Me.listBoxAutoComplete.Visible Then
                Select Case keyData
                    Case Keys.Tab, Keys.Return
                        Me.SelectItem
                        Me.HideIntellisenceDropDown
                        Return True
                    Case Keys.Escape
                        Me.HideIntellisenceDropDown
                        Return True
                End Select
            End If
            Return MyBase.ProcessCmdKey((msg), keyData)
        End Function

        Private Sub SelectItem()
            If (Me.listBoxAutoComplete.SelectedItems.Count > 0) Then
                Dim selectionStart As Integer = MyBase.SelectionStart
                Dim length As Integer = (selectionStart - Me.CurrentPrefix.Length)
                Dim startIndex As Integer = selectionStart
                If (startIndex >= Me.Text.Length) Then
                    startIndex = Me.Text.Length
                End If
                Dim str As String = Me.Text.Substring(0, length)
                Dim text As String = Me.listBoxAutoComplete.SelectedItems.Item(0).Text
                Dim str3 As String = Me.Text.Substring(startIndex, (Me.Text.Length - startIndex))
                Me.Text = (str & [text] & str3)
                MyBase.SelectionStart = (str.Length + [text].Length)
                MyBase.ScrollToCaret
                Me.oldSelectionStart = MyBase.SelectionStart
            End If
        End Sub

        Private Sub ShowIntellisenceDropDown(ByVal charIndex As Integer)
            If (Me.listBoxAutoComplete.Items.Count > 0) Then
                Dim positionFromCharIndex As Point = Me.GetPositionFromCharIndex((charIndex - 1))
                positionFromCharIndex.Y = (positionFromCharIndex.Y + (CInt(Math.Ceiling(CDbl(Me.Font.GetHeight))) + 2))
                positionFromCharIndex.X = (positionFromCharIndex.X - 6)
                If ((charIndex > 0) AndAlso (Me.Text.Chars((charIndex - 1)) = ChrW(10))) Then
                    positionFromCharIndex.Y = (positionFromCharIndex.Y + CInt(Math.Ceiling(CDbl(Me.Font.GetHeight))))
                    positionFromCharIndex.X = (Me.GetPositionFromCharIndex(0).X - 6)
                End If
                Dim point2 As Point = MyBase.TopLevelControl.PointToScreen(New Point(0, 0))
                Dim location As Point = MyBase.PointToScreen(positionFromCharIndex)
                location.Offset(-point2.X, -point2.Y)
                Dim size As Size = If(TypeOf MyBase.TopLevelControl Is Form, DirectCast(MyBase.TopLevelControl, Form).ClientSize, MyBase.TopLevelControl.Size)
                Dim rectangle As New Rectangle(location, Me.listBoxAutoComplete.Size)
                If (rectangle.Right > size.Width) Then
                    If (Me.listBoxAutoComplete.Size.Width > size.Width) Then
                        Me.listBoxAutoComplete.Size = New Size(size.Width, Me.listBoxAutoComplete.Height)
                    End If
                    location = New Point((size.Width - Me.listBoxAutoComplete.Size.Width), location.Y)
                End If
                If (rectangle.Bottom > size.Height) Then
                    Me.listBoxAutoComplete.Size = New Size(Me.listBoxAutoComplete.Width, (size.Height - rectangle.Top))
                End If
                Me.listBoxAutoComplete.Location = location
                Me.listBoxAutoComplete.BringToFront
                Me.listBoxAutoComplete.Show
            End If
        End Sub

        Private Sub ShowToolTip(ByVal charIndex As Integer, ByVal prefix As String)
            Dim positionFromCharIndex As Point = Me.GetPositionFromCharIndex((charIndex - 1))
            positionFromCharIndex.Y = (positionFromCharIndex.Y + (CInt(Math.Ceiling(CDbl(Me.Font.GetHeight))) + 2))
            positionFromCharIndex.X = (positionFromCharIndex.X - 6)
            Dim e As New AutoCompletionEventArgs { _
                .Prefix = prefix _
            }
            If (Not Me.PopulateToolTipList Is Nothing) Then
                Me.PopulateToolTipList.Invoke(Me, e)
                If (Not e.AutoCompleteValues Is Nothing) Then
                    Dim toolTipText As New StringBuilder
                    Dim flag As Boolean = True
                    Dim info As MemberInfo
                    For Each info In e.AutoCompleteValues
                        If flag Then
                            flag = False
                        Else
                            toolTipText.Append(ChrW(10))
                        End If
                        Dim parameters As ParameterInfo() = Nothing
                        Dim info2 As MethodInfo = [TryCast](info,MethodInfo)
                        If (Not info2 Is Nothing) Then
                            toolTipText.Append(RuleDecompiler.DecompileType(info2.ReturnType))
                            toolTipText.Append(" ")
                            toolTipText.Append(info2.Name)
                            toolTipText.Append("(")
                            parameters = info2.GetParameters
                        Else
                            Dim info3 As ConstructorInfo = DirectCast(info, ConstructorInfo)
                            toolTipText.Append(RuleDecompiler.DecompileType(info3.DeclaringType))
                            toolTipText.Append("(")
                            parameters = info3.GetParameters
                        End If
                        If ((Not parameters Is Nothing) AndAlso (parameters.Length > 0)) Then
                            Dim num As Integer = (parameters.Length - 1)
                            IntellisenseTextBox.AppendParameterInfo(toolTipText, parameters(0), (0 = num))
                            Dim i As Integer
                            For i = 1 To parameters.Length - 1
                                toolTipText.Append(", ")
                                IntellisenseTextBox.AppendParameterInfo(toolTipText, parameters(i), (i = num))
                            Next i
                        End If
                        toolTipText.Append(")")
                    Next
                    Me.toolTip.Show(toolTipText.ToString, Me, positionFromCharIndex)
                End If
            End If
        End Sub

        Private Sub UpdateAutoCompleteSelection(ByVal currentValue As String)
            Dim flag As Boolean = False
            If (String.IsNullOrEmpty(currentValue.Trim) AndAlso (Me.listBoxAutoComplete.Items.Count > 0)) Then
                flag = True
                Me.listBoxAutoComplete.Items.Item(0).Selected = True
                Me.listBoxAutoComplete.Items.Item(0).Focused = True
            Else
                Dim i As Integer
                For i = 0 To Me.listBoxAutoComplete.Items.Count - 1
                    If Me.listBoxAutoComplete.Items.Item(i).Text.StartsWith(currentValue, StringComparison.OrdinalIgnoreCase) Then
                        flag = True
                        Me.listBoxAutoComplete.Items.Item(i).Selected = True
                        Me.listBoxAutoComplete.Items.Item(i).Focused = True
                        Exit For
                    End If
                Next i
            End If
            If (Not flag AndAlso (Me.listBoxAutoComplete.SelectedItems.Count = 1)) Then
                Me.listBoxAutoComplete.SelectedItems.Item(0).Selected = False
            End If
        End Sub

        Private Sub UpdateIntellisenceDropDown(ByVal [text] As String)
            Dim e As New AutoCompletionEventArgs { _
                .Prefix = [text] _
            }
            If (Not Me.PopulateAutoCompleteList Is Nothing) Then
                Me.PopulateAutoCompleteList.Invoke(Me, e)
            End If
            Me.PopulateListBox(e.AutoCompleteValues)
        End Sub


        ' Properties
        Private ReadOnly Property CurrentPrefix As String
            Get
                Dim str As String = Me.Text.Substring(0, MyBase.SelectionStart)
                Dim num As Integer = str.LastIndexOfAny(" .()[]" & ChrW(9) & ChrW(13) & ChrW(10).ToCharArray)
                If (num >= 0) Then
                    Return str.Substring((num + 1))
                End If
                Return str
            End Get
        End Property


        ' Fields
        Private autoCompletionImageList As ImageList
        Private components As IContainer
        Private listBoxAutoComplete As ListView = New ListView
        Private oldSelectionStart As Integer
        Private toolTip As ToolTip

        ' Nested Types
        Private Enum memberIcons
            ' Fields
            [Default] = 0
            ExtensionMethod = 15
            InternalField = 12
            InternalMethod = 4
            InternalProperty = 8
            Keyword = 14
            PrivateField = 11
            PrivateMethod = 3
            PrivateProperty = 7
            ProtectedField = 13
            ProtectedMethod = 5
            ProtectedProperty = 9
            PublicField = 10
            PublicMethod = 2
            PublicProperty = 6
            Type = 1
        End Enum
    End Class
End Namespace

